//
//  CustomAnnotationView.m
//  Runone
//
//  Created by runone02 on 16/8/19.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "CustomAnnotationView.h"

#define kCalloutWidth       70.0
#define kCalloutHeight      50.0

@interface CustomAnnotationView ()

@property (nonatomic, strong, readwrite) CustomCallOutView *calloutView;

@end

@implementation CustomAnnotationView
//@synthesize calloutView = _calloutView;

#pragma mark - Override

- (void)setSelected:(BOOL)selected
{
    [self setSelected:selected animated:NO];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected) {
        return;
    }
    
    if (selected) {
        if (self.calloutView == nil) {
            self.calloutView = [[CustomCallOutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
                                                  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
        }
        
        self.calloutView.image = [UIImage imageNamed:@" "];
        self.calloutView.title = self.annotation.title;
        self.calloutView.subtitle = self.annotation.subtitle;
        NSLog(@"%@ %@ %@ %@",self.calloutView.title, self.annotation.title,self.calloutView.subtitle, self.annotation.subtitle);
        [self addSubview:self.calloutView];
    }
    else {
        [self.calloutView removeFromSuperview];
    }
    [super setSelected:selected animated:animated];
}

// 重新此函数，用以实现点击calloutView判断为点击该annotationView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL inside = [super pointInside:point withEvent:event];
    
    if (!inside && self.selected) {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    
    return inside;
}

// 传模型
-(void)setVehicleModel:(AllLastestVehicleDynamic *)vehicleModel {
    _vehicleModel = vehicleModel;
}

-(void)setDeviceModel:(DeviceBaseInfo *)deviceModel {
    _deviceModel = deviceModel;
}

- (void)setPileModel:(PileInfo *)pileModel {
    _pileModel = pileModel;
}

-(void)setCarManModel:(CarManagerModel *)carManModel {
    _carManModel = carManModel;
}



@end
